import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import Vue from 'vue'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  // NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  // const hasToken = getToken()
  next()

  // if (hasToken) {
  //   if (to.path === '/login') {
  //     // if is logged in, redirect to the home page
  //     next({ path: '/' })
  //     NProgress.done()
  //   } else {
  //     const hasGetUserInfo = store.getters.name
  //     if (hasGetUserInfo) {
  //       next()
  //     } else {
  //       try {
  //         // get user info
  //         await store.dispatch('user/getInfo')

  //         next()
  //       } catch (error) {
  //         // remove token and go to login page to re-login
  //         await store.dispatch('user/resetToken')
  //         Message.error(error || 'Has Error')
  //         next(`/login?redirect=${to.path}`)
  //         NProgress.done()
  //       }
  //     }
  //   }
  // } else {
  //   /* has no token*/

  //   if (whiteList.indexOf(to.path) !== -1) {
  //     // in the free login whitelist, go directly
  //     next()
  //   } else {
  //     // other pages that do not have permission to access are redirected to the login page.
  //     next(`/login?redirect=${to.path}`)
  //     NProgress.done()
  //   }
  // }
})

router.afterEach(() => {
  // finish progress bar
  // NProgress.done()
})



// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken1 } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'
// import Layout from '@/layout'
// NProgress.configure({ showSpinner: false }) // NProgress Configuration


// const whitechildren = [
//   '/login','/page/library/cont','/page/library/recommend',
// '/page/library/hot','/page/library/new','/page/av/cont','/page/av/recommend',
// '/page/av/hot','/page/av/new','/page/center/cont','/page/center/recommend',
// '/page/center/hotNew','/page/center/history'] 
// let flag = 0;
// router.beforeEach(async (to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)
//   // determine whether the user has logged in
//   const hasToken = getToken1()
//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()

//     } else {
//       const hasGetUserInfo = JSON.parse(localStorage.getItem('page'))
//       if (flag == 0) {
//         let rou = [];
//         for (let i = 0; i < hasGetUserInfo.length; i++) {
//           let menu = {
//             path: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].path + '/index' : hasGetUserInfo[i].path,
//             component: hasGetUserInfo[i].menu_pid === 0 ? Layout : hasGetUserInfo[i].children[0].component,
//             redirect: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].path + '/' + hasGetUserInfo[i].children[0].path : '',
//             children: [],
//             name: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].path.substr(1) : '',
//             meta: { title: hasGetUserInfo[i].menu_name, }
//           }

//           if (hasGetUserInfo[i].children && hasGetUserInfo[i].children.length > 0) {
//             for (let j = 0; j < hasGetUserInfo[i].children.length; j++) {
//               let obj = {
//                 path: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].children[j].path : 'index',
//                 component: hasGetUserInfo[i].children.length >= 2 ? (resolve) => require([`@/views${hasGetUserInfo[i].children[j].component}`], resolve) : (resolve) => require([`@/views${hasGetUserInfo[i].component}`], resolve),
//                 name: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].children[j].path : hasGetUserInfo[i].path.substr(1),
//                 meta: { title: hasGetUserInfo[i].children.length >= 2 ? hasGetUserInfo[i].children[j].menu_name : hasGetUserInfo[i].menu_name }
//               }
//               menu.children.push(obj)
//             }
//             router.options.routes.push(menu)
//           }
//         }
//         let obj = { path: '*', redirect: '/404', hidden: true }
//         let btn1 = [], btn2 = [], btn3 = [], btn4 = [], btn5 = [], btn6 = [], btn7 = [], btn8 = [], btn9 = [], btn10 = [], btn11 = [], btn12 = [], btn13 = [], btn14 = []
//         var arrayTwo = Object.values(store.getters.button.reduce((res, item) => {
//           res[item.menu_pid] ? res[item.menu_pid].push(item) : res[item.menu_pid] = [item];
//           return res;
//         }, {}));
//         for (let i = 0; i < arrayTwo.length; i++) {
//           for (let j = 0; j < arrayTwo[i].length; j++) {
//             let pid = arrayTwo[i][j].menu_pid
//             if (pid == 1) {
//               btn1.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 12) {
//               btn2.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 22) {
//               btn3.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 29) {
//               btn4.push(arrayTwo[i][j].menu_name)
//             } if (pid == 86) {
//               btn5.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 36) {
//               btn6.push(arrayTwo[i][j].menu_name)
//             } if (pid == 84) {
//               btn7.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 85) {
//               btn8.push(arrayTwo[i][j].menu_name)
//             } if (pid == 42) {
//               btn9.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 53) {
//               btn10.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 59) {
//               btn11.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 67) {
//               btn12.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 72) {
//               btn13.push(arrayTwo[i][j].menu_name)
//             }
//             if (pid == 77) {
//               btn14.push(arrayTwo[i][j].menu_name)
//             }
//           }

//         }
//         localStorage.setItem('btn1', btn1)
//         localStorage.setItem('btn2', btn2)
//         localStorage.setItem('btn3', btn3)
//         localStorage.setItem('btn4', btn4)
//         localStorage.setItem('btn5', btn5)
//         localStorage.setItem('btn6', btn6)
//         localStorage.setItem('btn7', btn7)
//         localStorage.setItem('btn8', btn8)
//         localStorage.setItem('btn9', btn9)
//         localStorage.setItem('btn10', btn10)
//         localStorage.setItem('btn11', btn11)
//         localStorage.setItem('btn12', btn12)
//         localStorage.setItem('btn13', btn13)
//         localStorage.setItem('btn14', btn14)
//         router.options.routes.push(obj)
      
//         router.selfaddRoutes(router.options.routes)

//         flag++
//         next({ ...to, replace: true })
//       }
//       else {
//         try {

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/
//     if (whitechildren.indexOf(to.path) !== -1) {
//       // in the free login whitechildren, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
